import 'package:auto_route/auto_route.dart';
import 'package:distribution_mall_flutter/pages/order/order_list/widgets/order_list_content.dart';
import 'package:distribution_mall_flutter/pages/order/order_list/widgets/order_list_nav.dart';
import 'package:distribution_mall_flutter/provider/order/common.dart';
import 'package:distribution_mall_flutter/provider/order/order_list.dart';
import 'package:distribution_mall_flutter/widgets/easy_refresh.dart';
import 'package:distribution_mall_flutter/widgets/shimmer.dart';
import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

@RoutePage()
class OrderListPage extends ConsumerStatefulWidget {
  const OrderListPage({super.key});

  @override
  createState() => _OrderListPage();
}

class _OrderListPage extends ConsumerState<OrderListPage> with RouteAware {
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      ref.read(orderListProvider.notifier).getTransportType();
    });
  }

  @override
  Widget build(BuildContext context) {
    bool firstLoading =
        ref.watch(orderListProvider.select((value) => value.firstLoading));
    return Scaffold(
      body: EPEasyRefresh(onInit: () {
        // 第一次loading
        // ref.read(orderListProvider.notifier).setParams(null);
        ref.read(orderListProvider.notifier).setFirstLoading();
        // 拉去订单列表数据
        ref.read(orderListProvider.notifier).initData({}, true);
        // 取消原因列表
        ref.read(orderCommonProvider.notifier).getAfterSaleReason();
      }, onRefresh: () async {
        return await ref.read(orderListProvider.notifier).refresh({}, false);
      }, onLoad: () async {
        return await ref.read(orderListProvider.notifier).onLoadMore();
      }, childBuilder: (context, physics) {
        return NestedScrollView(
          physics: physics,
          headerSliverBuilder: (context, innerBoxIsScrolled) {
            return [
              OrderListNav(),
              const HeaderLocator.sliver(clearExtent: false)
            ];
          },
          body: EPShimmer(
            enabled: firstLoading,
            child: OrderListContent(physics: physics),
          ),
        );
      }),
    );
  }

  // @override
  // void deactivate() {
  //   super.deactivate();
  //   debugPrint('+++++++++++++++++');
  //   ref.read(orderListProvider.notifier).setParams(null);
  //   // super.dispose();
  // }
}
